<template>
	<view class="content" id="content">
		<scroll-view scroll-y class="left-aside">
			<view v-for="item in flist.category" :key="item.id" class="f-item b-b"
				:class="{active: item.id === currentId}" @click="tabtap(item)">
				{{item.name}}
			</view>
		</scroll-view>
		<scroll-view scroll-with-animation scroll-y class="right-aside" @scroll="asideScroll"
			:scroll-top="tabScrollTop">
			<view class="s-list">
				<view class="t-list" v-if="tlist&&tlist.length>0">
					<view @click="navToList(titem,'&cname='+titem.name)" class="t-item" v-for="titem in tlist"
						:key="titem.id">
						<image :src="titem.thumb"></image>
						<text>{{titem.name}}</text>
					</view>
				</view>
				<view v-else class="noneData">没有更多分类了~</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sizeCalcState: false,
				tabScrollTop: 0,
				currentId: "",
				flist: [],
				slist: [],
				tlist: [],
				mid: 0,
				code: ''
			}
		},
		async onLoad(options) {
			var that = this;
			if(options.code){
				this.code = options.code;
			}
			this.$axios('Util/categorynew', "POST", 'shop').then(res => {
				if (res.data.code == 200) {
					that.flist = res.data.data;
					that.tlist = res.data.data.category[0].children
					that.currentId = res.data.data.category[0].id;
					// 根据分类名称找到对应分类并选中
					if(options.cname){
						that.flist.category.forEach((item)=>{
							if(item.name == options.cname){
								that.currentId = item.id;
							}
						})
					}
					this.curSale();
				}
			}, (error) => {

			})
			if(options.mid){
				this.mid = options.mid;
			}
			// await this.$onLaunched;
			if(!uni.getStorageSync('token')){
				this.getToken();
			}
		},
		onReady() {
			// 判断是不是ios，是否发生白屏
			uni.getSystemInfo({
				success: (res) => {
					if (res.platform == 'ios') {
						const query = uni.createSelectorQuery().in(this);
						var isRecovery = true;
						query
							.select('#content')
							.boundingClientRect(data => {
								if (data) isRecovery = false;
							})
							.exec();
						var timer = setTimeout(() => {
							if (isRecovery) {
								uni.reLaunch({
									url: '/pages/category/category'
								})
								clearTimeout(timer);
								timer = null;
							}
						}, 500)
					}
				},
			})
		},
		methods: {
			getToken(){
				/*#ifdef H5*/
				this.$axios('login/autoGzlogin','POST','member',{
					mid: this.mid,
					code: this.code,
				}).then(res=>{
					if(res.data.code == 300001){
						this.$navTo(res.data.data);
					}else if(res.data.code == 200){
						uni.setStorageSync('token', res.data.data);
						uni.setStorageSync('Jh_recotabshops', 1);
						this.getSharePath();
					}
				})
				/*#endif*/
				/*#ifndef H5*/
				uni.login({
					provider: 'weixin',
					success:res=> {
						this.$axios('login/autologin','POST','member',{
							mid: this.mid,
							code: res.code
						}).then(res=>{
							if(res.data.code == 200){
								uni.setStorageSync('token', res.data.data);
								uni.setStorageSync('Jh_recotabshops', 1);
								this.getSharePath();
							}
						})
					}
				})
				/*#endif*/
			},
			//一级分类点击
			tabtap(item) {
				if (!this.sizeCalcState) {
					this.calcSize();
				}
				this.tlist = item.children
				this.currentId = item.id;
				let index = this.slist.findIndex(sitem => sitem.pid === item.id);
				this.curSale();
				// this.tabScrollTop = this.slist[index].top;
			},
			//右侧栏滚动
			asideScroll(e) {
				if (!this.sizeCalcState) {
					this.calcSize();
				}
				let scrollTop = e.detail.scrollTop;
				let tabs = this.slist.filter(item => item.top <= scrollTop).reverse();
				if (tabs.length > 0) {
					this.currentId = tabs[0].pid;
				}
			},
			//计算右侧栏每个tab的高度等信息
			calcSize() {
				let h = 0;
				this.slist.forEach(item => {
					let view = uni.createSelectorQuery().select("#main-" + item.id);
					view.fields({
						size: true
					}, data => {
						item.top = h;
						h += data.height;
						item.bottom = h;
					}).exec();
				})
				this.sizeCalcState = true;
			},
			navToList(i, e) {
				var that = this
				uni.navigateTo({
					url: "/pages/goodlist/goodlist?brandid=" + i.id + e
				})
			},
			curSale(){
				var that = this
				this.$axios('Util/categorylist', "POST", 'shop',{cate:this.currentId}).then(res => {
					if (res.data.code == 200) {
						// that.tlist=[];
						that.tlist = res.data.data.list;
					}
				}, (error) => {
				
				})
				
			}
		}
	}
</script>

<style lang='scss'>
	page,
	.content {
		height: 100%;
		background-color: #f8f8f8;
	}

	.content {
		display: flex;
	}
	.noneData{
		padding-top: 20rpx;
		color: #999;
		text-align: center;
	}

	.left-aside {
		flex-shrink: 0;
		width: 200upx;
		height: 100%;
		background-color: #fff;
	}

	.f-item {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100upx;
		font-size: 28upx;
		color: $font-color-base;
		position: relative;

		&.active {
			color: $base-color;
			background: #f8f8f8;

			&:before {
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				height: 36upx;
				width: 8upx;
				background-color: $base-color;
				border-radius: 0 4px 4px 0;
				opacity: .8;
			}
		}
	}

	.right-aside {
		flex: 1;
		overflow: hidden;
		padding-left: 20upx;
	}

	.s-item {
		display: flex;
		align-items: center;
		height: 70upx;
		padding-top: 8upx;
		font-size: 28upx;
		color: $font-color-dark;
	}
	.t-list {
		display: flex;
		flex-wrap: wrap;
		width: 100%;
		background: #fff;
		padding-top: 12upx;

		&:after {
			content: '';
			flex: 99;
			height: 0;
		}
	}

	.t-item {
		flex-shrink: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		width: 176upx;
		font-size: 26upx;
		color: #666;
		padding-bottom: 20upx;
		image {
			width: 140upx;
			height: 140upx;
			border-radius: 12rpx;
		}
	}
</style>
